#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include <list>
#include <cstdint>
#include <iostream>
#include <iterator>

class Player : public sf::Drawable
{
	private:
		uint8_t hp;
		sf::RectangleShape collider;

		virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
		{
			target.draw(this->collider);
		}

	public:
		static const uint8_t maxHP = 5;

		Player() : hp(5), collider({50, 50})
		{
			this->collider.setFillColor(sf::Color::Green);
		}

		bool collision(const sf::RectangleShape& object)
		{
			return this->collider.getGlobalBounds().intersects(object.getGlobalBounds());
		}

		void move(sf::Vector2f vector)
		{
			this->collider.move(vector);
		}

};

class Game
{
	private:
		sf::RenderWindow* window;

		Player player;
		std::list<sf::RectangleShape> objects;

		bool quit = false;

		void draw()
		{
			this->window->clear(sf::Color::Black);
			this->window->draw(this->player);
			for(const auto& object : this->objects)
			{
				this->window->draw(object);
			}
			this->window->display();
		}

		void processCollisions()
		{
			for (std::list<sf::RectangleShape>::iterator it = this->objects.begin(); it != this->objects.end();)
			{
				if(this->player.collision(*it))
				{
					std::cout << "collision" << std::endl;
					it = this->objects.erase(it);
				}
				++it;
			}
		}

		void processEvents()
		{
			sf::Event event;
			while (window->pollEvent(event))
			{
				if(event.type == sf::Event::Closed)
				{
					this->quit = true;
				}
				else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
				{
					this->player.move(sf::Vector2f(0, -5));
				}
				else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
				{
					this->player.move(sf::Vector2f(0, 5));
				}
				else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
				{
					this->player.move(sf::Vector2f(-5, 0));
				}
				else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
				{
					this->player.move(sf::Vector2f(5, 0));
				}
			}
		}

	public:
		Game(sf::RenderWindow* window) : window(window)
		{
			sf::RectangleShape r1({20, 20});
			r1.setFillColor(sf::Color::Red);
			r1.setPosition({300, 300});

			sf::RectangleShape r2({40, 40});
			r2.setFillColor(sf::Color::Yellow);
			r2.setPosition({100, 200});

			sf::RectangleShape r3({10, 10});
			r3.setFillColor(sf::Color::Magenta);
			r3.setPosition({20, 600});

			sf::RectangleShape r4({20, 20});
			r4.setFillColor(sf::Color::Cyan);
			r4.setPosition({800, 700});

			this->objects.push_back(r1);
			this->objects.push_back(r2);
			this->objects.push_back(r3);
			this->objects.push_back(r4);
		}

		void run()
		{
			while(!this->quit)
			{
				this->processEvents();
				this->processCollisions();
				this->draw();
			}
		}


};
int main(int argc, char *argv[])
{
	sf::RenderWindow window(sf::VideoMode(1280, 720), "sfml-template");
	window.setFramerateLimit(30);

	Game game(&window);
	game.run();

	window.close();

	return 0;
}
